<template>
  <div class="categories-container">
    <h2>活动分类</h2>
    <div class="category-actions">
      <button class="add-category-btn">添加分类</button>
    </div>
    
    <div class="categories-list">
      <div v-for="category in categories" :key="category.id" class="category-card">
        <div class="category-info">
          <h3>{{ category.name }}</h3>
          <p class="category-description">{{ category.description }}</p>
          <p class="event-count">包含活动数: {{ category.eventCount }}</p>
        </div>
        <div class="category-operations">
          <button class="edit-btn">编辑</button>
          <button class="delete-btn">删除</button>
        </div>
      </div>
    </div>
    
    <!-- 添加/编辑分类模态框 -->
    <div v-if="showModal" class="modal-overlay" @click.self="closeModal">
      <div class="modal-content">
        <h3>{{ isEditMode ? '编辑分类' : '添加分类' }}</h3>
        <div class="form-group">
          <label for="categoryName">分类名称</label>
          <input type="text" id="categoryName" v-model="currentCategory.name" placeholder="请输入分类名称">
        </div>
        <div class="form-group">
          <label for="categoryDescription">分类描述</label>
          <textarea id="categoryDescription" v-model="currentCategory.description" rows="3" placeholder="请输入分类描述"></textarea>
        </div>
        <div class="modal-actions">
          <button class="cancel-btn" @click="closeModal">取消</button>
          <button class="save-btn" @click="saveCategory">{{ isEditMode ? '保存' : '添加' }}</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'

export default {
  name: 'EventCategories',
  setup() {
    // 模拟分类数据
    const categories = ref([
      { id: 1, name: '技术', description: '技术相关活动', eventCount: 42 },
      { id: 2, name: '营销', description: '营销推广活动', eventCount: 28 },
      { id: 3, name: '社交', description: '社交交流活动', eventCount: 19 },
      { id: 4, name: '教育', description: '教育培训活动', eventCount: 15 }
    ])
    
    const showModal = ref(false)
    const isEditMode = ref(false)
    const currentCategory = ref({
      name: '',
      description: ''
    })
    
    const addCategory = () => {
      isEditMode.value = false
      currentCategory.value = { name: '', description: '' }
      showModal.value = true
    }
    
    const editCategory = (category) => {
      isEditMode.value = true
      currentCategory.value = { ...category }
      showModal.value = true
    }
    
    const deleteCategory = (categoryId) => {
      if (confirm('确定要删除此分类吗？')) {
        const index = categories.value.findIndex(cat => cat.id === categoryId)
        if (index !== -1) {
          categories.value.splice(index, 1)
        }
      }
    }
    
    const saveCategory = () => {
      if (isEditMode.value) {
        const index = categories.value.findIndex(cat => cat.id === currentCategory.value.id)
        if (index !== -1) {
          categories.value[index] = { ...currentCategory.value }
        }
      } else {
        const newCategory = {
          id: Date.now(),
          ...currentCategory.value,
          eventCount: 0
        }
        categories.value.push(newCategory)
      }
      closeModal()
    }
    
    const closeModal = () => {
      showModal.value = false
    }
    
    return {
      categories,
      showModal,
      isEditMode,
      currentCategory,
      addCategory,
      editCategory,
      deleteCategory,
      saveCategory,
      closeModal
    }
  }
}
</script>

<style scoped>
.categories-container {
  padding: 20px;
}

h2 {
  margin-bottom: 24px;
  color: #333;
}

.category-actions {
  margin-bottom: 24px;
  text-align: right;
}

.add-category-btn {
  padding: 8px 16px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.add-category-btn:hover {
  background-color: #40a9ff;
}

.categories-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.category-card {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.category-info h3 {
  margin-top: 0;
  margin-bottom: 8px;
  color: #333;
}

.category-description {
  color: #666;
  font-size: 14px;
  margin-bottom: 12px;
}

.event-count {
  color: #999;
  font-size: 12px;
  margin: 0;
}

.category-operations {
  display: flex;
  gap: 8px;
}

.edit-btn, .delete-btn {
  padding: 4px 12px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}

.edit-btn {
  background-color: #1890ff;
  color: white;
}

.edit-btn:hover {
  background-color: #40a9ff;
}

.delete-btn {
  background-color: #ff4d4f;
  color: white;
}

.delete-btn:hover {
  background-color: #ff7875;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  padding: 24px;
  border-radius: 8px;
  width: 90%;
  max-width: 500px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.modal-content h3 {
  margin-top: 0;
  margin-bottom: 20px;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
  font-size: 14px;
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 14px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 24px;
}

.cancel-btn, .save-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.cancel-btn {
  background-color: #f5f5f5;
  color: #333;
}

.cancel-btn:hover {
  background-color: #e8e8e8;
}

.save-btn {
  background-color: #1890ff;
  color: white;
}

.save-btn:hover {
  background-color: #40a9ff;
}
</style>